package com.springboot.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.springboot.blog.beans.Blog;
import com.springboot.blog.beans.BlogGonggao;
import com.springboot.blog.beans.PageBlog;
import com.springboot.blog.beans.YouQing;
import com.springboot.blog.config.utils.R;
import com.springboot.blog.dao.BlogGgDao;
import com.springboot.blog.service.GonggaoService;
import net.sf.json.JSONArray;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("admin")
public class GongGaoController {

    @Resource
    private GonggaoService gonggaoService;
    @Resource
    private BlogGgDao blogGgDao;

    @RequestMapping("addGonggaos")
    public R addyouqing(String blogGgtitle,String blogGgcontent){
        BlogGonggao gonggao = new BlogGonggao();
        gonggao.setBlogGgtitle(blogGgtitle);
        gonggao.setBlogGgcontent(blogGgcontent);
        int insert = blogGgDao.insert(gonggao);
        if (insert>0){
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("allgonggao")
    private String allgonggao(Integer page,Integer limit){
        PageBlog<BlogGonggao> pageBlog = gonggaoService.page(page, limit);
        int count = blogGgDao.selectCount(null);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("count",count);
        jsonObject.put("data",pageBlog);
        return jsonObject.toString();
    }

    @RequestMapping("deleteGg")
    public R deleteGg(String delete){
        List<BlogGonggao> list2=(List<BlogGonggao>) JSONArray.toList(JSONArray.fromObject(delete), BlogGonggao.class);
        List<Integer> userList = new ArrayList<>();
        for (BlogGonggao gonggao : list2){
            userList.add(gonggao.getBlogGgid());
        }
        int deleteUser = blogGgDao.deleteBatchIds(userList);
        if (deleteUser>0) {
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("deleteByIdGg")
    public R deleteByIdGg(String blogGgid){
        int delete = blogGgDao.deleteById(Integer.parseInt(blogGgid));
        if (delete>0){
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("updateByIdGg")
    public R updateByIdGg(String blogGgid,String blogGgtitle,String blogGgcontent){
        BlogGonggao gonggao = new BlogGonggao();
        gonggao.setBlogGgid(Integer.parseInt(blogGgid));
        gonggao.setBlogGgtitle(blogGgtitle);
        gonggao.setBlogGgcontent(blogGgcontent);
        int update = blogGgDao.updateById(gonggao);
        if (update>0){
            return R.ok();
        }
        return R.error();
    }

}
